//
//  ZBZYPriceVC.swift
//  ANOA
//
//  Created by 邱仙凯 on 2022/4/6.
//  Copyright © 2025 aowang. All rights reserved.
//

import UIKit
 
class ZBZYPriceVC: TenderBaseTableController,UISearchBarDelegate {
    
    var page = 1
    
     var dataArray = [ZtbContentModel]()
    var z_zb_id = "" // 投标记录使用
    
    var listType = TBandJPType.TB
    var btnflag = "1"
    
    var billTitle = ""
    var followTag = ""
  
    private var searchBar:UISearchBar?
    
    
    init(listType:TBandJPType = .TB,z_zb_id:String = "",searchTitle:String = "",followTag:String = "0") {
        super.init(nibName: nil, bundle: nil)
        self.listType = listType
        self.followTag = followTag
        self.z_zb_id = z_zb_id
        self.helper.filterPa!.searchTitle = searchTitle
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "出价记录"
        
        let rightbtn = UIButton.creat4Item(image: UIImage(named: "all_record")!) { _ in
            getRecordListtt(zDtradeType: "",zb_id: self.z_zb_id, staff_id: "",billtitle: self.billTitle,zb_source: "0",s_data_source: "0")

        
        }
        let right = UIBarButtonItem(customView:rightbtn)
        self.navigationItem.rightBarButtonItem = right
       
        helper.callTableRefreshClosure = {
            self.tableView.mj_header?.beginRefreshing()
            
        }
        
        tableView.estimatedRowHeight = 50
        tableView.rowHeight = UITableView.automaticDimension
        
        tableView.mj_header = KSRefreshHeader { [unowned self] in
            self.page = 1
            self.network()
        }
        tableView.mj_footer = KSRefreshFooter { [unowned self] in
            self.page += 1
            self.network()
        }
        tableView.mj_header?.beginRefreshing()
        
        tableView.register(ZBZYPriceCell.self, forCellReuseIdentifier: ZBZYPriceCell.identifier)
        tableView.register(TenderChujiaRCell.self, forCellReuseIdentifier: TenderChujiaRCell.identifier)

        

        
        
        
        tableView.tableFooterView = UIView()
        
        
    }
    
  
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return self.dataArray.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   
            let one = self.dataArray[indexPath.row]
       
                guard let cell = tableView.dequeueReusableCell(withIdentifier: TenderChujiaRCell.identifier, for: indexPath) as? TenderChujiaRCell else {
                    fatalError("cell error")
                }
             
                cell.makeConstrainsts()
 
                one.innerType = .chujiaRecordKHW
                cell.oper_btn.addTouchUpInSideAction {[one] _ in
                    
                    if one.zStatus.unwrap == "1" {
                        let nav = BaseNavigationController()
                        let vc = ZyaCarInfoVC()
                        vc.dataArray = one.carInfo
                        nav.viewControllers = [vc]
                        self.present_FullScreen(to: nav)
                    }
                    
                
                    

                }
                cell.one = one
        cell.mychujiaBtn.addTouchUpInSideAction { [one] _ in
            
            
          
            getRecordListtt(zDtradeType: one.zDtradeType.unwrap,zb_id: one.zZbId.unwrap, staff_id: one.zStaffId.unwrap,billtitle: self.billTitle,zb_source: "0",s_data_source: "0")
                 
               
           
           
        }
                
                return cell
         
            
          
         
        
        
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
     
        self.helper.go2TenderTbDetailController(listType: .TB, controller:self,idkey:self.dataArray[indexPath.row].idKey.unwrap,innerType:.chujiaRecordKHW)
            
    }
    
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let one = self.dataArray[indexPath.row]
        
        return CGFloat(40 + (one.bidInfoArr?.count ?? 0)*30)
    }
    
    func network () {
        
        
        
        let api = API.queryBidInfoList(page: "\(page)", size: "10", jp_id: z_zb_id, z_title: self.helper.filterPa!.searchTitle, z_sort: "1",s_data_source: "0")
        
 
        
        KSNetwork.shared.doRequest(api,showHud: false) { [weak self] (data:BaseResult<ZtbContentModel>) in
            guard let `self` = self else { return }
            if data.flag == TRUE {
                if let infos = data.infoArrCombine {
                    if self.page == 1 {
                        self.dataArray.removeAll()
                    }
                    self.dataArray += infos
                    self.tableView.reloadData()
                    self.tableView.mj_header?.endRefreshing()
                    if infos.count < 10 {
                        self.tableView.mj_footer?.endRefreshingWithNoMoreData()
                    } else {
                        self.tableView.mj_footer?.endRefreshing()
                    }
                }
            } else {
                KSToast(data.errorCombine.unwrap)
            }
        }
    }
    
    
    deinit {
        print("tenderfirst 销毁")
    }
}


extension ZBZYPriceVC:XYXFilterMenuDataSource {
    // 头部显示要改变的时候回调此方法
    func menu(_ menu: XYXFilterMenu, shouldChange title: String, for indexPath: XYXFilterIndexPath) -> String? {
        
        let titlesStr = self.listType == .TB ? self.helper.titles_zb[indexPath.column!] : self.helper.titles_jp[indexPath.column!]
        var theTitle = title
        if theTitle == "全部." {
            theTitle = provinceArr[indexPath.row!]
        }
        if theTitle == "全部" {
            theTitle = titlesStr
        }
        return theTitle
    }
    // 每个列展示的数组
    func menu(_ menu: XYXFilterMenu, numberOfRowsAt indexPath: XYXFilterIndexPath) -> Int {
        guard indexPath.column != nil else {
            return 0
        }
        switch indexPath.column! {
        case 0:
            return self.listType == .TB ? zhuangtaiArr_zb.count : zhuangtaiArr_jp.count
        case 1:
            return provinceArr.count
        case 2:
            if self.listType == .TB {
                return self.helper.source_tb_status.count
                
            } else {
                return self.helper.source_jp_status.count
            }
            
        case 3:
            return 1
        default:
            return 0
        }
    }
    
    func menu(_ menu: XYXFilterMenu, numberOfItemsAt indexPath: XYXFilterIndexPath) -> Int {
        guard indexPath.column != nil && indexPath.row != nil else {
            return 0
        }
        switch indexPath.column! {
        case 1:
            return cityDic[provinceArr[indexPath.row!]]!.count
        case 3:
            return 1
        default:
            return 0
        }
    }
    // 各个过滤项的 标题数组
    func menu(_ menu: XYXFilterMenu, titleOfRowAt indexPath: XYXFilterIndexPath) -> String {
        let defaultString = ""
        guard indexPath.column != nil && indexPath.row != nil else {
            return defaultString
        }
        switch indexPath.column! {
        case 0: // 状态（招标的时候）
            return self.listType == .TB ? zhuangtaiArr_zb[indexPath.row!] : zhuangtaiArr_jp[indexPath.row!]
        case 1: // 区域
            return provinceArr[indexPath.row!]
            
        case 2: // 时间
            if self.listType == .TB {
                return self.helper.source_tb_status[indexPath.row!]
            } else {
                return self.helper.source_jp_status[indexPath.row!]
            }
            
        default:
            return defaultString
        }
    }
    
    func menu(_ menu: XYXFilterMenu, titleOfItemAt indexPath: XYXFilterIndexPath) -> String {
        let defaultString = ""
        guard indexPath.column != nil && indexPath.row != nil && indexPath.item != nil else {
            return defaultString
        }
        if indexPath.column == 1 {
            var cityArr = cityDic[provinceArr[indexPath.row!]]!
            if cityArr.count > 1 {
                cityArr.insert("全部.", at: 0)
            } else {
                
            }
            return cityArr[indexPath.item!]
        }
        return defaultString
    }
    // 大菜单数量
    func numberOfColumns(menu: XYXFilterMenu) -> Int {
        return self.listType == .TB ? self.helper.titles_zb.count : self.helper.titles_jp.count
    }
    // 大标题数据
    func menu(_ menu: XYXFilterMenu, titleOfColumnAt index: Int) -> String {
        return self.listType == .TB ? self.helper.titles_zb[index] : self.helper.titles_jp[index]
    }
    // 菜单的样式，单列表 多列表   collectionview等
    func menu(_ menu: XYXFilterMenu, typeOfColumn: Int) -> XYXFilterView.ColumnType.RawValue {
        switch typeOfColumn {
        case 1:
            return XYXFilterView.ColumnType.TableViewTwo.rawValue
        case 0,2:
            return XYXFilterView.ColumnType.TableViewOne.rawValue
        default:
            return XYXFilterView.ColumnType.collectionViewType.rawValue
        }
    }
    
    func menu(_ menu: XYXFilterMenu, widthOf tableView: UITableView, at column: Int) -> CGFloat {
        if column == 1 {
            if tableView.tag == XYXFilterView.firstTabViewTag{
                return XYX_SCREEN_WIDTH * 0.35
            }else if tableView.tag == XYXFilterView.secondTabViewTag{
                return XYX_SCREEN_WIDTH * 0.65
            }
        }
        return XYX_SCREEN_WIDTH/2
    }
    
    // 菜单向下展开的高度
    func menu(_ menu: XYXFilterMenu, filterContentHeightAt column: Int) -> CGFloat {
        if column == 3 {
            return 200.0
        } else {
            return 300.0
        }
        
    }
    
}

extension ZBZYPriceVC:XYXFilterMenuDelegate {
    func menu(_ menu:XYXFilterMenu, tapIndex: Int) {
        print(tapIndex)
    }
    func menu(_ menu: XYXFilterMenu, tapIndexShowEnable: Int) -> Bool {
        print(tapIndexShowEnable)
        if tapIndexShowEnable == 3 {
            self.helper.listFilterAction(listType: self.listType)
            return false
        } else {
            return true
        }
    }
}

